package mines.fila3.letstalk.activities;

import java.io.IOException;

import mines.fila3.letstalk.R;
import mines.fila3.letstalk.params.LetsTalkConst;
import mines.fila3.letstalk.tools.LetsTalkNetwork;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

public class EnvoyerMessageActivity extends Activity {

	EditText messageToSend;
	Button btnSend;
	ProgressBar progressBar;
	SendMessageTask sendMessageTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_envoyer_message);
		bindComponents();
		addListeners();
	}

	private void bindComponents() {
		messageToSend = (EditText) findViewById(R.id.messageToSend);
		btnSend = (Button) findViewById(R.id.btnSend);
		progressBar = (ProgressBar) findViewById(R.id.progressbar);
	}

	private void addListeners() {
		btnSend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				if (LetsTalkNetwork
						.isConnected((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE))) {

					if (sendMessageTask == null || sendMessageTask.getStatus() == Status.FINISHED) {
						sendMessageTask = new SendMessageTask();
					}
					if (sendMessageTask.getStatus() == Status.PENDING) {
						sendMessageTask.execute();
					}
				} else {
					Toast.makeText(getApplicationContext(), R.string.msg_no_connection, Toast.LENGTH_SHORT)
							.show();
				}
			}
		});
	}

	private class SendMessageTask extends AsyncTask<String, Void, Boolean> {

		@Override
		protected void onPreExecute() {
			progressBar.setVisibility(View.VISIBLE);
		}

		@Override
		protected Boolean doInBackground(String... params) {
			String message = messageToSend.getText().toString();
			String formatMessage = Uri.encode(message);

			SharedPreferences prefs = getSharedPreferences(LetsTalkConst.USER_CREDENTIALS, MODE_PRIVATE);
			String username = prefs.getString(LetsTalkConst.KEY_USERNAME, LetsTalkConst.EMPTY_STRING);
			String password = prefs.getString(LetsTalkConst.KEY_PASSWORD, LetsTalkConst.EMPTY_STRING);

			// appeler webservice
			try {
				DefaultHttpClient client = new DefaultHttpClient();
				HttpGet request = new HttpGet(LetsTalkConst.getAdressHTTP(LetsTalkConst.QUERY_MESSAGE,
						username, password) + "/" + formatMessage);
				client.execute(request);
				return true;
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			return false;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			// exécuté dans l'UIThread
			// rendre la roue invisble
			progressBar.setVisibility(View.INVISIBLE);

			if (result) {
				Toast.makeText(getApplicationContext(), getString(R.string.msg_successfully_sent),
						Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(getApplicationContext(), getString(R.string.msg_not_sent), Toast.LENGTH_SHORT)
						.show();
			}
			EnvoyerMessageActivity.this.finish();
		}
	}
}